package servlet;

import entity.RainQuality;
import service.IRainQualityService;
import service.impl.RainQualityServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/addRainQuality")
public class AddRainQualityServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private IRainQualityService rainQualityService = new RainQualityServiceImpl();

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("AddRainQuality.jsp").forward(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        RainQuality rainQuality = new RainQuality();
        rainQuality.setDistrictName(request.getParameter("districtName"));
        rainQuality.setMonitorTime(request.getParameter("monitorTime"));
        rainQuality.setRain(Integer.parseInt(request.getParameter("rain")));
        rainQuality.setMonitoringStation(request.getParameter("monitoringStation"));
        rainQuality.setMonitoringAddress(request.getParameter("monitoringAddress"));

        try {
            boolean result = rainQualityService.add(rainQuality);
            if (result) {
                response.sendRedirect("RainQualityListServlet");
            } else {
                response.sendRedirect("error.jsp");
            }
        } catch (Exception e) {
            e.printStackTrace();
            response.sendRedirect("error.jsp");
        }
    }
}
